package com.example.allplan.ShuJuKuCaoZuo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.allplan.JavaBean.NaoZhongJavaBean;
import com.example.allplan.JavaBean.YiGeChouQian;
import com.example.allplan.SQhelper.NaoZhongSQhelper;
import com.example.allplan.SQhelper.SuoYouChouQianSQhelper;

import java.util.ArrayList;

public class SuoYouChouQianSQCaoZuo {
    public SuoYouChouQianSQhelper msq;
    public Context con;
    public SQLiteDatabase mdb;
    public SuoYouChouQianSQCaoZuo(Context context){
        con=context;
        msq=new SuoYouChouQianSQhelper(con,"suo_you_chou_qian",null,1);
    }
    public  synchronized void openRead(){
        mdb=msq.getReadableDatabase();
    }
    public synchronized void openWrite(){
        mdb=msq.getWritableDatabase();
    }
    public synchronized void close(){
        mdb.close();
    }
    public synchronized ArrayList<YiGeChouQian> readall(){
        ArrayList<YiGeChouQian> mList=new ArrayList<YiGeChouQian>();
        openWrite();
        Cursor cursor = mdb.query("chou_qian_biao", new String[]{"id","pai_xu_you_biao","chou_qian_ming","wen_jian_ming"}, null, null, null, null, "id");
        while (cursor.moveToNext()) {
            mList.add(new YiGeChouQian(cursor.getInt(cursor.getColumnIndex("id")),cursor.getInt(cursor.getColumnIndex("pai_xu_you_biao")),cursor.getString(cursor.getColumnIndex("chou_qian_ming")),cursor.getString(cursor.getColumnIndex("wen_jian_ming"))));
        }
        close();
        return mList;
    }
    public synchronized void writeone(YiGeChouQian o){
        openWrite();
        mdb.insert("chou_qian_biao",null,bean_to_values(o));
        close();
    }
    public synchronized void deleteone(YiGeChouQian o){
        openWrite();
        mdb.delete("chou_qian_biao","id=?",new String[]{o.getId()+""});
        close();
    }
    public synchronized void updateoneonlytwo(YiGeChouQian o){
        openWrite();
        mdb.update("chou_qian_biao",bean_to_valuesonlytwo(o),"id=?",new String[]{o.getId()+""});
        close();
    }
    public synchronized void updateoneonlypaixu(YiGeChouQian o){
        openWrite();
        mdb.update("chou_qian_biao",bean_to_valuesonlypaixu(o),"id=?",new String[]{o.getId()+""});
        close();
    }
    private ContentValues bean_to_valuesonlytwo(YiGeChouQian o) {
        ContentValues values=new ContentValues();
        values.put("id",o.getId());
        values.put("chou_qian_ming",o.getMing_cheng());
        return values;
    }
    private ContentValues bean_to_valuesonlypaixu(YiGeChouQian o) {
        ContentValues values=new ContentValues();
        values.put("id",o.getId());
        values.put("pai_xu_you_biao",o.getPai_xu_you_biao());
        return values;
    }

    private ContentValues bean_to_values(YiGeChouQian o) {
        ContentValues values=new ContentValues();
        values.put("id",o.getId());
        values.put("pai_xu_you_biao",o.getPai_xu_you_biao());
        values.put("chou_qian_ming",o.getMing_cheng());
        values.put("wen_jian_ming",o.getWen_jian_ming());
        return values;


    }
}
